package com.example.yskjproject.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.view.View;
import android.widget.VideoView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.yskjproject.R;
import com.tencent.cos.xml.utils.StringUtils;
import com.xzh.imagepicker.bean.MediaFile;

import java.io.IOException;
import java.util.List;

/**
 * PicVideoAdapter
 * <p>
 * 作者：zx
 * 创建时间：2024/4/30
 */
public class PicVideoAdapter  extends BaseQuickAdapter<MediaFile, BaseViewHolder> {

    private Context context;
    List<MediaFile> data;

    String meOrOther;

    private String strHide;

    public PicVideoAdapter(@Nullable List<MediaFile> data, Context context,String meOrOther) {
        super(R.layout.item_pic_video, data);
        this.context = context;
        this.data = data;
        this.meOrOther = meOrOther;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setHide(String strHide){
        this.strHide = strHide;
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void convert(BaseViewHolder helper, MediaFile item) {
        AppCompatImageView img = helper.getView(R.id.img_pic);
        if (item.getFolderName().equals("picture") || item.getFolderName().equals("Pictures") ){
            Glide.with(context).load(item.getPath())
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(10)))//圆角半径
                    .into(img);
        }else if(item.getFolderName().equals("Movies")){
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            try {
                // 设置视频源
                retriever.setDataSource(item.getPath());
                // 获取第一帧
                Bitmap firstFrame = retriever.getFrameAtTime(0);
                // 使用第一帧
                Glide.with(context).load(firstFrame)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(40)))//圆角半径
                        .into(img);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 释放资源
                try {
                    retriever.release();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        helper.addOnClickListener(R.id.item_pic_video);
        AppCompatImageView delete =  helper.getView(R.id.delect_img);
        if (!StringUtils.isEmpty(meOrOther)){
            delete.setVisibility(View.GONE);
        }else {
            helper.addOnClickListener(R.id.delect_img);
        }


    }
}
